﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAO;
//теперь вроде все дочищено. офигенчик)а то статическое поле накапливало лишнюю хуйню.т.ак, ща посмотрим что еще надо
namespace Sklad.WindowsApplication
{
    public partial class AddReceiveForm : Form
    {
        public static List<TempClass> itemlist; 
        public static BindingSource bs = new BindingSource();

        public AddReceiveForm()
        {
            InitializeComponent();
            itemlist = new List<TempClass>(); 
            this.cb_rec_empl.Items.AddRange(AdminMainForm.dr.EmployeeRepository.GetAll().ToArray());
            this.cb_provider.Items.AddRange(AdminMainForm.dr.ProviderRepository.GetAll().ToArray());
            UpdateDataGrid();
            this.dataGridView1.DataSource = bs;
            this.dataGridView1.Columns[0].Visible = false;
            this.dataGridView1.Columns[1].Visible = false;
        }

        private void bt_add_product_Click(object sender, EventArgs e)
        {
            AddProductToForm aptf = new AddProductToForm(FormType.AddReceiveForm);
            aptf.Visible = true;
            aptf.FormClosing += aptf_FormClosing;
            this.Visible = false;
        }

        void aptf_FormClosing(object sender, FormClosingEventArgs e)
        {
            UpdateDataGrid();
            this.Visible = true;
        }
   
        void UpdateDataGrid()
        {
            int cnt = 0;
            foreach (var item in itemlist)
            {
                item.local_id = cnt++;
            }
            bs.DataSource = new BindingList<TempClass>(itemlist);
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            int idEmpl = (this.cb_rec_empl.SelectedItem as Employer).Id;
            int idProv = (this.cb_provider.SelectedItem as Provider).Id;
            DateTime dt = DateTime.Parse(this.dp_receive.Text);
            AdminMainForm.dr.AcceptanceRepository.Add(new Acceptance()
            {
                Id_Employer = idEmpl,
                Id_Provider = idProv,
                Date = dt
            });
            var last = AdminMainForm.dr.AcceptanceRepository.GetAll().Last();

            foreach (var item in itemlist)
            {
                AdminMainForm.dr.AcceptanceProductRep.Add(
                       new Acceptance_Product()
                       {
                           Id_Acceptance = last.Id,
                           Id_Product = item.Id,
                           Count = item.Count
                       });
            }
            this.Close();
        }

        private void bt_delete_product_Click(object sender, EventArgs e)
        {
            var temp = bs.Current as TempClass;

            itemlist.RemoveAll(x => x.local_id == temp.local_id);
            UpdateDataGrid();
        }
    }

    public class TempClass
    {
        public int local_id { get; set; }
        public int Id { get; set; }
        public string Name { get; set; }
        public int Count { get; set; }
    }
}
